
package com.hisense.settings.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;

import com.google.android.tv.mediadevices.ActionEvent;
import com.google.android.tv.mediadevices.ActionEvent.State;
import com.hisense.settings.device.PairingDevice;
/*import com.marvell.hdmicec.CECDevInfo;
import com.marvell.hdmicec.CecManager;
import com.marvell.hdmicec.CecManager.CecSrvConnectionListener;
import com.marvell.hdmicec.DeviceType;
import com.marvell.hdmicec.ITvCecService;
import com.marvell.tv.TVAVSettingsConstants.TvSource;
import com.marvell.tv.TVAVSettingsConstants.TvSourceType;
import com.marvell.tv.cec.types.KeyPressMode;*/

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public final class CecUtils {
    private static final String TAG = "CecUtils";
    public static int CEC_SETTING_HDMI_CEC = 0;
    public static int CEC_SETTING_TV_AUTO_POWER_ON = 1;
    public static int CEC_SETTING_DEVICE_AUTO_POWER_OFF = 2;
    public static int CEC_SETTING_SAC = 3;
    public static String CEC_MSG_REFRESH_INENT_ACTION = "usdtv.hdmicec.intent.REFRESH_OSD";

    public static String CEC_ACTION_ACTIVE_SOURCE = "intent.hdmicec.ACTIVE_SOURCE";
    public static String CEC_ACTION_INACTIVE_SOURCE = "intent.hdmicec.INACTIVE_SOURCE";
    public static String CEC_ACTION_AUDIO_STATUS_UPDATE = "intent.hdmicec.ARC_AUDIO_STATUS_UPDATE";
    public static String CEC_ACTION_AUDIO_STATUS_REMOVE = "intent.hdmicec.ARC_AUDIO_STATUS_REMOVE";
    public static String CEC_ACTION_OSD_DISPLAY_NOTIFY = "intent.hdmicec.SHOW_OSD";
    public static String CEC_ACTION_CEC_ON = "intent.hdmicec.CEC_ON";
    public static String CEC_ACTION_TV_AS_ACTIVE = "intent.hdmicec.TV_AS_ACTIVE";
    public static String CEC_ACTION_TV_ROUTING_CHANGE = "intent.hdmicec.TV_ROUTHING_CHANGE";
    public static String CEC_ACTION_ACTIVATE_CEC_SAC_DEVICES =
            "intent.hdmicec.ACTIVATE_CEC_SAC_DEVICES";
    public static String CEC_INTENT_HDMI_HUTPLUG_STATUS_CHANGED =
            "com.marvell.hdmicec.HUTPLUG_STATUS_CHANGED";
    public static String CEC_ACTION_HDMI_CONNECTING_CHANGED =
            "intent.hdmicec.HDMI_CONNECTING_CHANGED";

    public static final String EXTRA_OSD_STRING_DISPLAY_MODE = "osd_string_display_mode";
    public static final String KEY_SOURCE_ID = "sourceid";

    public static final int FIRST_HDMI_PORT = 1;
    public static final int LAST_HDMI_PORT = 4;
    public static final int HDMI_ARC_PORT = 1;

    //private static ITvCecService mITvCecService = null;
    //private static CecSrvConnectionListener mCecListener = null;

    /**
     * The broadcast action requesting update SAC status
     */
    public static final String HDMI_SAC_AUDIO_STATUS_ACTION =
            "com.marvell.hdmicec.ARC_AUDIO_STATUS_UPDATE";

    /*public static void setCecSrvConnectionListener(CecSrvConnectionListener listener) {
        mCecListener = listener;
    }

    public static CecSrvConnectionListener getCecSrvConnectionListener() {
        return mCecListener;
    }

    public static ITvCecService getITvCecService() {
        return mITvCecService;
    }

    public static void setITvCecService(ITvCecService cecService) {
        mITvCecService = cecService;
    }

    public static void connectCecService() {
        CecManager cecManager = Wrapper.getWrapperInstance().getCecManager();
        Log.d(TAG, "connectCecService mCecManager=" + cecManager + "  mCecListener="
                + mCecListener);
        if (cecManager != null && mCecListener != null) {
            cecManager.registerCecSrvConnectionListener(mCecListener);
            cecManager.connect();
        }
    }

    public static void unregisterCecServiceListener() {
        CecManager cecManager = Wrapper.getWrapperInstance().getCecManager();
        if (cecManager != null && mCecListener != null) {
            cecManager.unregisterCecSrvConnectionListener(mCecListener);
        }
    }

    public static void disconnectCecService() {
        CecManager cecManager = Wrapper.getWrapperInstance().getCecManager();
        if (cecManager != null) {
            cecManager.disconnect();
        }
    }

    public static List<CECDevInfo> getConnectedDevicesAtPort(int portNumber) {
        try {
            List<CECDevInfo> devList = new ArrayList<CECDevInfo>();
            if (portNumber >= FIRST_HDMI_PORT && portNumber <= LAST_HDMI_PORT) {
                List<CECDevInfo> cecList = mITvCecService.getListOfConnectedCECDevices();
                if (cecList != null && cecList.size() > 0) {
                    CECDevInfo info = null;
                    for (int i = 0; i < cecList.size(); i++) {
                        info = cecList.get(i);
                        int port = info.getPortNumber();
                        if (port == portNumber) {
                            devList.add(info);
                        }
                    }

                    return devList;
                }

            }
        } catch (Exception e) {
            Log.i(TAG, "Exception in function getConnectedDevicesAtPort " + e);
        }
        return null;
    }*/

    /**
     * check if can pass volume key to audio system.
     * @return [a,b,c] if a=0 then should not pass, if a=1 should pass, and b,c
     *         is the logic address and port
     */
    public static int[] shouldControlConnectedAudioSystemVolume() {
        int[] audevinfo = {
                0, -1, -1
        };
       /* int port = -1;
        int logaddr = -1;

        Wrapper wrapper = Wrapper.getWrapperInstance();

        if (wrapper.getHdmiCec() == 0 || wrapper.getSystemAudioControl() == 0
                || mITvCecService == null) {
            return audevinfo;
        }

        try {
            List<CECDevInfo> cecList = mITvCecService.getListOfConnectedCECDevices();
            if (cecList != null && cecList.size() > 0) {
                CECDevInfo info = null;
                for (int i = 0; i < cecList.size(); i++) {
                    info = cecList.get(i);
                    if (info.getDeviceType() == DeviceType.AudioDevice) {
                        port = info.getPortNumber();
                        logaddr = info.getDeviceAddress();
                        break;
                    }
                }

            }

        } catch (RemoteException exp) {
            exp.printStackTrace();
        }

        if (logaddr != -1) {
            int curport = getCurrentHdmiInputPort();
            if ((port == HDMI_ARC_PORT) || (port == curport)) {
                audevinfo[0] = 1;
                audevinfo[1] = logaddr;
                audevinfo[2] = port;
            } else {
                Log.d(TAG, "Not match the SAC volume control conditions"
                        + ",audio device port:" + port + ",curport:" + curport);
            }
        } else {
            Log.d(TAG, "No audio device");
        }*/

        return audevinfo;
    }

    public static int shouldKeyPassthrough(Context context) {
        int activeDev = - 1;
        int firstPlaybackDev = -1;
        /*CECDevInfo info = null;
        List<CECDevInfo> cecList;
        
        if (context == null || Wrapper.getWrapperInstance().getHdmiCec() == 0) {
            return activeDev;
        }

        // check if current source is HDMI source
        int port = getCurrentHdmiInputPort();
        if (port < FIRST_HDMI_PORT || port > LAST_HDMI_PORT) {
            return activeDev;
        }

        cecList = getConnectedDevicesAtPort(port);
        if(cecList != null && cecList.size() > 0) {
	        if (mITvCecService != null) {
	            try {
	            	activeDev = mITvCecService.getActiveSourceAddress();
	            } catch (RemoteException e) {
	                e.printStackTrace();
	            }
	        }
	        *//**
	         *	Check if the active source is connecting at current port,
	         *	if yes return it,if no find out the first playback device,
	         *//*
            for (int i = 0; i < cecList.size(); i++) {
                info = cecList.get(i);
                int addr = info.getDeviceAddress();
                if(addr == activeDev) {
                	firstPlaybackDev = activeDev;
                	break;
                } else if((info.getDeviceType() == DeviceType.PLAYBACK_DEVICE) && (firstPlaybackDev == -1)) {
                	firstPlaybackDev = info.getDeviceAddress();
                }
            }
            
        }*/
        return firstPlaybackDev;
    }

    public static HashMap<Integer, String> cecKeyCodeMapping;
    static {
        cecKeyCodeMapping = new HashMap<Integer, String>();
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_VOLUME_UP, "VOLUME_UP");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_VOLUME_DOWN, "VOLUME_DOWN");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_VOLUME_MUTE, "MUTE");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_ENTER, "SELECT");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_DPAD_UP, "UP");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_DPAD_DOWN, "DOWN");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_DPAD_LEFT, "LEFT");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_DPAD_RIGHT, "RIGHT");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MENU, "TOP_MENU");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_BACK, "EXIT");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_0, "NUM_0");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_1, "NUM_1");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_2, "NUM_2");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_3, "NUM_3");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_4, "NUM_4");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_5, "NUM_5");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_6, "NUM_6");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_7, "NUM_7");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_8, "NUM_8");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_9, "NUM_9");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_PLAY, "PLAY");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_PAUSE, "PAUSE");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_STOP, "STOP");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_RECORD, "RECORD");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_REWIND, "REWIND");
        cecKeyCodeMapping.put(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, "FAST_FORWARD");
    }

    public static String getCECActionEventOrdinal(KeyEvent event) {
        int keycode = event.getKeyCode();
        String codeString = cecKeyCodeMapping.get(keycode);
        if (codeString == null)
            codeString = "INVALID";
        Log.d(TAG, "getCECActionEventOrdinal=" + codeString);
        return codeString;
    }

    public static int getCurrentHdmiInputPort() {
        int port = 0;
        /*int curSrc = Wrapper.getWrapperInstance().getCurrentSource();
        if (curSrc == TvSource.TV_SourceHdmi0.value()) {
            port = 1;
        } else if (curSrc == TvSource.TV_SourceHdmi1.value()) {
            port = 2;
        } else if (curSrc == TvSource.TV_SourceHdmi2.value()) {
            port = 3;
        } else if (curSrc == TvSource.TV_SourceHdmi3.value()) {
            port = 4;
        }*/
        return port;
    }

    public static int getPhyaddrByPort(int port) {
        switch (port) {
            case 1:
                return 0x1000;
            case 2:
                return 0x2000;
            case 3:
                return 0x3000;
            case 4:
                return 0x4000;
            default:
                return 0;
        }
    }

    public static int getKeyPressMode(ActionEvent event) {
        /*State state = event.getState();
        int keyPressMode = KeyPressMode.INVALID_STATE;
        if (state == State.DOWN) {
            keyPressMode = KeyPressMode.KEY_PRESSED;
        } else if (state == State.UP) {
            keyPressMode = KeyPressMode.KEY_RELEASED;
        }
        return keyPressMode;*/
    	return 0;//add for AU
    }

    public static int numberOfCecDeviceConnected(int port) {
       /* List<CECDevInfo> list = null;
        if (port >= CecUtils.FIRST_HDMI_PORT && port <= CecUtils.LAST_HDMI_PORT) {
            list = CecUtils.getConnectedDevicesAtPort(port);
        }
        if (list != null) {
            return list.size();
        }*/
        return 0;
    }

    public static int getHdmiPortbyDeviceId(String deviceId) {
        if (deviceId.equals(PairingDevice.HDMI1_DEVICE_ID)) {
            return PairingDevice.getPortId(deviceId);
        } else if (deviceId.equals(PairingDevice.HDMI2_DEVICE_ID)) {
            return PairingDevice.getPortId(deviceId);
        } else if (deviceId.equals(PairingDevice.HDMI3_DEVICE_ID)) {
            return PairingDevice.getPortId(deviceId);
        } else if (deviceId.equals(PairingDevice.HDMI4_DEVICE_ID)) {
            return PairingDevice.getPortId(deviceId);
        }
        // Non - HDMI port
        return 0;
    }

    /**
     * @param deviceId
     *            :the new device that will switch
     * @return true if switch from HDMI to Non-HDMI
     */
    public static boolean switchFromHdmiToNonHdmi(String deviceId) {

        /*int srctype = Wrapper.getWrapperInstance().getCurrentSourceType();
        if (srctype == TvSourceType.TV_SOURCE_TypeHdmi.value()) {
            if (deviceId.equals(PairingDevice.COMPONENT_DEVICE_ID)
                    || deviceId.equals(PairingDevice.COMPOSITE_DEVICE_ID)
                    || deviceId.equals(PairingDevice.TUNER_DEVICE_ID)
                    || deviceId.equals(PairingDevice.VGA_DEVICE_ID)) {
                return true;
            }
        }*/
        return false;
    }

    public static String getDeviceIdByHdmiPort(int portNumber) {
        Log.i(TAG, " @@@@ getDeviceId method  @@@@@ ----- PortNumber == " + portNumber);

        if (portNumber == PairingDevice.getPortId(PairingDevice.HDMI1_DEVICE_ID)) {
            return PairingDevice.HDMI1_DEVICE_ID;
        } else if (portNumber == PairingDevice.getPortId(PairingDevice.HDMI2_DEVICE_ID)) {
            return PairingDevice.HDMI2_DEVICE_ID;
        } else if (portNumber == PairingDevice.getPortId(PairingDevice.HDMI3_DEVICE_ID)) {
            return PairingDevice.HDMI3_DEVICE_ID;
        } else if (portNumber == PairingDevice.getPortId(PairingDevice.HDMI4_DEVICE_ID)) {
            return PairingDevice.HDMI4_DEVICE_ID;
        } else {
            return null;
        }
    }
}
